
$(document).ready(function() {
	
	videojs.options.flash.swf = templateUrl+"/video-js.swf";
	
	//videojs.addTextTrack([{ kind: 'captions', label: 'English2', language: 'en', srclang: 'en', src: 'http://photo.bongngo.vn/subtitles/bn/movie/2014/5/the-bank-job-2008-720p-bluray-dts-x264-ebp(1).srt' }]);
	//videojs.trigger("textTracksChanged");
	if(typeof(isOld)!="undefined"){
		playeriframe=$('#videoiframe');
		
		player = videojs('videoframe', {
			techOrder: ['flash', 'html5','youtube'],
			height:300,
			autoplay: false
		});
		
		
		

		player.ready(function(){
			
			//alert(1);
			loading=$('.vjs-loading-spinner');
			loading.after('<div id="vjs-text-custom-message" class="vjs-text-track-display"></div>');			
			displayMessage=$('#vjs-text-custom-message');			
			displayMessage.css('font-size','14px');
			bigplaybutton=$('.vjs-big-play-button');
			bigplaybutton.on('click',function(){
				bigplaybutton.hide();
				loading.show();
			});
			
				
			changepart(defaultpart);
			
		});
		
		player.on('error',function(){	
			changeOnError();
		});
		
		player.on('loadeddata',function(){
			isPlaying=1;
			clearInterval(intervalId);
			//alert(1);
		});	
		
		player.on('play',function(){
			clearInterval(intervalId);
		});	
	}
	});
var error=0;
var mvid=0;
var currentquality=0;
var intervalId=0;
var isPlaying=0;
function checkPlayerIsPlaying(){
	intervalId=setInterval(function(){
		//alert('check');
		if(loading.is(':hidden') && isPlaying==0){
			//replay
			changeOnError();			
		}
	}, 3000);
}
function changeOnError(){
	if(error>=2){
		//get next link
		//alert('error='+error);
		error=0;
		changepart(defaultpart,currentquality,mvid);		
		clearInterval(intervalId);
		
	}
	else{
		player.play();
		error++;
	}
}
function changeQuality(quality){
	currentquality=quality;
	changepart(defaultpart,quality);
}
function changepart(part,quality,errorid){
	
	defaultpart=part;
	var data={'action': 'changepart', 'data': part};
	if(typeof(quality)!="undefined"){
		data['quality']=quality;
	}
	if(typeof(errorid)!="undefined"){
		data['errorid']=errorid;
	}
	if(!player.paused())
		player.pause();
	isPlaying=0;
	displayMessage.html('');
	bigplaybutton.hide();
	loading.show();
	jQuery.post(
			hosturl,data, 
		    function(response){
		       //$('votecontent').html(response);
		       
		    	
		       if(response!=null && response!='')
		       {
		    	   showMovie(response);
		    	}
		       else{
		    	   loading.hide();
		    	   displayMessage.html("Server đang bận, mời bạn hãy quay lại lúc khác nhé.");
					
			       }
		    
		    }
		);
}

function showMovie(data){
	var obj = jQuery.parseJSON(data);	
	var type='video/webm';
	error=0;
	if(obj.default==0 || obj.default==null){
		loading.hide();
		displayMessage.html("Server đang bận, mời bạn hãy quay lại lúc khác nhé.");
	}
	else{
		mvid=obj.default.id;
		//test
		
		//player.trigger("textTracksChanged");
		//document.getElementById('videoiframe').innerHTML='<track label="English Captions" srclang="en" kind="captions" src="http://localhost:8080/xpct/wp-content/themes/xpct/demo.captions.vtt" type="text/vtt" default />';
		//obj.default.type='video/mpeg4';
		//obj.default.url='http://117.103.204.156:1935/bongngovod26/mp4:./7e4cb8fd29a565ed01523c4d803e1521/537d39a1/Encoded/TvShow/2014/2/6/20496_vietsub-dadwherearewegoingep56-ss2-360kpop-_720_480.mp4/playlist.m3u8';
		//obj.default.url='http://117.103.204.156:1935/bongngovod2/mp4:./c1f85b6a1df1db9926fc4fcb86cb2807/537d3e25/Encoded/Movie/2014/5/21/23599_i-give-my-first-love-to-you-2009-720p-bluray-ac3-avc-abd_480_480.mp4/playlist.m3u8';
		//obj.default.url='http://117.103.204.131:1935/bongngovod2/mp4:./bc6c3cc9520681c2a610b58c436c1943/537d3e25/Encoded/Movie/2014/5/21/23599_i-give-my-first-love-to-you-2009-720p-bluray-ac3-avc-abd_240_240.mp4/playlist.m3u8';
		//
		//obj.default.type='bongngovn/mpeg4';
		//obj.default.url='http://bongngo.vn/tvshow/watch/1/170-1457/dad-where-are-you-going-season-2-bo-oi-minh-di-dau-the-phan-tap-56-tiep-theo-season-1';
		
		//alert(obj.default.url);
		//show quality
		if(obj.quality!=undefined)$('#qualitycontent').html(obj.quality);
		else $('#qualitycontent').html('');
		if(obj.default.type=='application/x-shockwave-flash')type='video/flv';
		if(obj.default.type=='video/mpeg4')type='video/mp4';
		if(obj.default.type=='youtube' || obj.default.type=='dailymotion'){
			type='video/'+obj.default.type;		
		}
		if(obj.default.type==''){
			type="video/youtube";		
		}
		if(obj.default.type=='iframe'){
			$('#videoframe').height(0);
			playeriframe.show();
			playeriframe.attr('src',obj.default.url);
		}
		
		else{
			playeriframe.hide();
			$('#videoframe').height(300);
			//player.currentTime(0);	
			player.src({ type: type, src: obj.default.url });
			//subtitle
			if(obj.default.sub!=undefined){
				player.addTextTracks([{ kind: 'captions', label: 'Vietnam', language: 'vi', srclang: 'vi', src: obj.default.sub}]);
				//player.trigger("textTracksChanged");
				var arrtrac=player.textTracks();			
				player.showTextTrack(arrtrac[1].id());
			}
			if(obj.default.type=='youtube'){
				loading.hide();
				bigplaybutton.show();
			}
			else{
				player.play();
				checkPlayerIsPlaying();
			}
				
			
		}
	}
	
}